package com.ctshk.rpc.air.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.air.dto.AirFlightDataDto;
import com.ctshk.rpc.air.entity.AirFlight;
import com.ctshk.rpc.air.mapper.AirFlightMapper;
import com.ctshk.rpc.air.req.*;
import com.ctshk.rpc.air.service.IAirFlightService;
import com.google.common.collect.Lists;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 航班表 服务实现类
 * </p>
 *
 * @author lihang
 * @since 2021-03-04
 */
@DubboService
public class AirFlightServiceImpl extends ServiceImpl<AirFlightMapper, AirFlight> implements IAirFlightService {


    @Autowired
    private AirFlightMapper airFlightMapper;




    @Override
    public Result add(AirFlightAddReq req, TokenUser tokenUser){
        AirFlight airFlight = EntityUtil.copy(req, AirFlight.class);
        LocalDateTime now = LocalDateTime.now();
        airFlight.setCreateId(tokenUser.getId());
        airFlight.setModifiedId(tokenUser.getId());
        airFlight.setGmtCreate(now);
        airFlight.setGmtModified(now);
        airFlight.setIsDeleted(IsDeletedCode.NO.getCode());
        airFlight.setCabinClass(JSON.toJSONString(req.getAirFlightCabinDataList()));
        long id = IdWorker.getId();
        airFlight.setId(id);
        airFlightMapper.insert(airFlight);
        return Result.success(id);
    }

    @Override
    public Result<AirFlightDataDto> query(AirFlightQueryReq req) {
        AirFlight airFlight = airFlightMapper.selectById(req.getId());
        AirFlightDataDto airFlightDataDto = EntityUtil.copy(airFlight,AirFlightDataDto.class);
        airFlightDataDto.setAirFlightCabinDataList(JSON.parseArray(airFlight.getCabinClass(), AirFlightCabinData.class));
        return Result.success(airFlightDataDto);
    }

    @Override
    public PageResponse<AirFlightDataDto> list(AirFlightListReq req) {
        Page<AirFlight> page = new Page<>(req.getPageNo(), req.getPageSize());
        QueryWrapper<AirFlight> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AirFlight::getIsDeleted,IsDeletedCode.NO.getCode());
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(req.getAirplaneType()),AirFlight::getAirplaneType,req.getAirplaneType());
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(req.getCompanyId()),AirFlight::getCompanyId,req.getCompanyId());
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(req.getArrCityId()),AirFlight::getArrCityId,req.getArrCityId());
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(req.getDepCityId()),AirFlight::getDepCityId,req.getDepCityId());
        queryWrapper.lambda().eq(StringUtils.isNotBlank(req.getFlightNumber()),AirFlight::getFlightNumber,req.getFlightNumber());
        queryWrapper.lambda().eq(StringUtils.isNotBlank(req.getArrAirportId()),AirFlight::getArrAirportId,req.getArrAirportId());
        queryWrapper.lambda().eq(StringUtils.isNotBlank(req.getDepAirportId()),AirFlight::getDepAirportId,req.getDepAirportId());
        queryWrapper.lambda().le(ObjectUtils.isNotEmpty(req.getArrTime()),AirFlight::getArrTime,req.getArrTime());
        queryWrapper.lambda().ge(ObjectUtils.isNotEmpty(req.getDepTime()),AirFlight::getDepTime,req.getDepTime());
        queryWrapper.lambda().orderByDesc(AirFlight::getGmtCreate);
        Page<AirFlight> iPage = airFlightMapper.selectPage(page, queryWrapper);
        List<AirFlight> records = iPage.getRecords();
        if(CollectionUtils.isEmpty(records)){
            return  new PageResponse<AirFlightDataDto>(new ArrayList<>(), iPage.getCurrent() == 1,iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());
        }
        List<AirFlightDataDto> airFlightDataDtoList = Lists.newArrayList();
        for (AirFlight record : records) {
            AirFlightDataDto airFlightDataDto = EntityUtil.copy(record,AirFlightDataDto.class);
            airFlightDataDto.setAirFlightCabinDataList(JSON.parseArray(record.getCabinClass(), AirFlightCabinData.class));
            airFlightDataDtoList.add(airFlightDataDto);
        }
        return new PageResponse<AirFlightDataDto>(airFlightDataDtoList, iPage.getCurrent() == 1,iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());
    }


    @Override
    public Result edit(AirFlightUpdateReq req, TokenUser tokenUser) {
        AirFlight airFlight = EntityUtil.copy(req,AirFlight.class);
        airFlight.setModifiedId(tokenUser.getId());
        airFlight.setGmtModified(LocalDateTime.now());
        airFlight.setCabinClass(JSON.toJSONString(req.getAirFlightCabinDataList()));
        airFlightMapper.updateById(airFlight);
        return Result.success(req.getId());
    }

    @Override
    public Result editStatus(AirFlightUpdateReq req, TokenUser tokenUser) {
        UpdateWrapper<AirFlight> updateWrapper  = new UpdateWrapper<>();
        updateWrapper.lambda().eq(AirFlight::getId,req.getId());
        updateWrapper.lambda().set(AirFlight::getModifiedId,tokenUser.getId());
        updateWrapper.lambda().set(AirFlight::getGmtModified,LocalDateTime.now());
        updateWrapper.lambda().set(AirFlight::getStatus,req.getStatus());
        airFlightMapper.update(null,updateWrapper);
        return Result.success(req.getId());
    }

    @Override
    public List<AirFlightDataDto> listByIds(List<Long> ids) {
        QueryWrapper<AirFlight> airFlightQueryWrapper =new QueryWrapper<>();
        airFlightQueryWrapper.lambda().in(AirFlight::getId,ids);
        List<AirFlight> airFlightList = airFlightMapper.selectList(airFlightQueryWrapper);
        List<AirFlightDataDto> airFlightDataDtoList = EntityUtil.copyList(airFlightList,AirFlightDataDto.class);
        return airFlightDataDtoList;
    }
}
